package com.qf.service.impl;

import com.qf.constant.RabbitMQConstants;
import com.qf.model.StandardSubmit;
import com.qf.service.StrategyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 路由策略
 * @author lenovo
 * @version V1.0
 * @Date 2021/11/22 21:37
 */
@Component(value = "router")
@Slf4j
public class RouterFilterServiceImpl implements StrategyService {

    //用于创建队列和发送消息到队列中
    @Autowired
    private AmqpAdmin amqpAdmin;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public Boolean execute(StandardSubmit standardSubmit) {
        log.info("【策略模块 - 路由策略】开始执行，短信对象内容 standardSubmit={}",standardSubmit);

        //1.封装队列的名称
        String queueName = RabbitMQConstants.TOPIC_SMS_GATEWAY + standardSubmit.getGatewayID();

        //2.创建指定的队列,如果没有则创建
        amqpAdmin.declareQueue(QueueBuilder.durable(queueName).build());

        //3.发送消息到指定的队列（使用默认的交换机）
        rabbitTemplate.convertAndSend(queueName, standardSubmit);

        log.info("【策略模块 - 路由策略】成功执行完毕 standardSubmit={}",standardSubmit);
        //4.记录日志
        return true;
    }
}
